package com.md.album.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.player.ui.recently.adapter.RecentlyAudioAdapter;
import com.md.album.R;
import com.md.album.db.SelfAlbumManager;
import com.md.basedpc.base.BaseFragment;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractSelectAudioFragment extends BaseFragment implements OnItemClickListener, View.OnClickListener {

    protected TextView tv_batch_left, tv_batch_title, tv_batch_right,tvTotalNum,tvOrder;
    protected LinearLayout lltReverseOrder;
    protected boolean isNormalOrder = true;
    protected Map<String, Audio> mapSel = new ConcurrentHashMap<>();
    protected RecentlyAudioAdapter taskItemAdapter;
    protected List<Audio> audioList = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.fragment_self_audios_batch_delete;
    }

    public Map<String, Audio> getMapSel() {
        return mapSel;
    }

    /**
     * 移除操作
     *
     * @param t
     */
    public void remove(Audio t) {
        taskItemAdapter.remove(t);
        audioList.remove(t);
        refreshSelect();
        if (taskItemAdapter.getData().size() == 0) {
            getActivity().finish();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        tv_batch_left = view.findViewById(R.id.tv_batch_left);
        tv_batch_title = view.findViewById(R.id.tv_batch_title);
        tv_batch_right = view.findViewById(R.id.tv_batch_right);
        tvTotalNum = view.findViewById(R.id.tvTotalNum);
        lltReverseOrder = view.findViewById(R.id.lltReverseOrder);
        tvOrder = view.findViewById(R.id.tvOrder);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        taskItemAdapter = new RecentlyAudioAdapter(true,true, mapSel);
        recyclerView.setAdapter(taskItemAdapter);
        taskItemAdapter.setOnItemClickListener(this);
        tv_batch_left.setOnClickListener(this);
        tv_batch_right.setOnClickListener(this);
        lltReverseOrder.setOnClickListener(this);
        taskItemAdapter.setNewInstance(audioList);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        refreshSelect();
        tvTotalNum.setText("共"+audioList.size()+"首");
        tvOrder.setText(getResources().getString(isNormalOrder? R.string.album_normal_order:R.string.album_reverse_order));
    }

    protected abstract void initData();

    private void refreshSelect() {
        if (mapSel.size() == audioList.size()) {
            tv_batch_left.setText("取消全选");
        } else {
            tv_batch_left.setText("全选");
        }
        if (mapSel.size() > 0) {
            tv_batch_title.setText("已选定" + mapSel.size() + "首");
        } else {
            tv_batch_title.setText("批量操作");
        }
        if (onBatchSelectLisenter != null) {
            onBatchSelectLisenter.onSelect(mapSel.size(), audioList.size());
        }
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.tv_batch_left) {
            if (mapSel.size() == audioList.size()) {
                mapSel.clear();
            } else {
                // 全选
                for (Audio tasksManagerModel : audioList) {
                    mapSel.put(tasksManagerModel.getId(), tasksManagerModel);
                }
            }
            taskItemAdapter.notifyDataSetChanged();
            refreshSelect();
        } else if (_id == R.id.tv_batch_right) {
            getActivity().finish();
        }else if(_id==R.id.lltReverseOrder){
            isNormalOrder = !isNormalOrder;
            List<Audio> tempList = new ArrayList<>();
            for (int i=audioList.size()-1;i>=0;i--){
                tempList.add(audioList.get(i));
            }
            audioList.clear();
            audioList.addAll(tempList);
            taskItemAdapter.notifyDataSetChanged();
            tvOrder.setText(getResources().getString(isNormalOrder? R.string.album_normal_order:R.string.album_reverse_order));
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Audio tasksManagerModel = taskItemAdapter.getItem(position);
        if (mapSel.containsKey(tasksManagerModel.getId())) {
            mapSel.remove(tasksManagerModel.getId());
        } else {
            mapSel.put(tasksManagerModel.getId(), tasksManagerModel);
        }
        taskItemAdapter.notifyItemChanged(position);
        refreshSelect();
    }

    OnBatchSelectLisenter onBatchSelectLisenter;

    public void setOnBatchSelectLisenter(OnBatchSelectLisenter onBatchSelectLisenter) {
        this.onBatchSelectLisenter = onBatchSelectLisenter;
    }

    public interface OnBatchSelectLisenter {
        void onSelect(int count, int total);
    }


}
